<?php session_start();
if (isset($_SESSION['lang']))
    require_once "./language_files/callForPapers_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/callForPapers_en.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <link href="style3.css" rel="stylesheet" type="text/css" />
<?php include 'jquery.php'; ?>
        <script type="text/javascript" src="functions.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                check_top();
            });
        </script>
        <title><?php echo $lang['callforpaper_title']; ?></title>
    </head>
    <body>
        <form action="" method="post">
            <?php
            //Author: Aya El Mobayad
            //The page functions.php is included so that the functions created inside of it can be
            // accessible, and used within the page

            require_once 'functions.php';
            // A condition is added, checking whether the conference ID was passed from the previous page
            //in a get request using the isset function.
            if (isset($_GET['conference_id'])) {
                //The conference ID is retrieved from the previous page using a get request
                // and assigned to a variable
                $confID = intval($_GET['conference_id']);
            } else if (isset($_POST['confID'])) {
                $confID = intval($_POST['confID']);
            }
            //specifies the connection to the server, as well as the username and password and opens it.
            //The conference long name is retrieved by calling the get_conference_name function from
            //functions.php and passing it the conference id of the conference for which the name
            //is needed, as a parameter.
            $confName = get_conference_name($confID);
            echo"
	</br><span class=breadcrumbs><a href='subscriberConferencePage.php?conference_id={$confID}'>" . $confName . "</a>" . " > " . $lang['callforpaper_title'] . " </span> </br>";
            //The name retrieved using the previous function is printed at the top of the page
            //in a heading element
            print "<h1>{$confName}</h1>";
            //a heading is printed at the top of the page underneath the conference title.
            ?><h2><?php echo $lang['callforpaper_title']; ?></h2><?php
            //a textarea is created for the call for papers text, and validates using the isAlphabet javascript function.
            ?><?php
            echo $lang['announcement'];
            echo $lang['hint'];
            ?>
            <br /><textarea id="announcement" name ="announcement" cols="50" rows="7" onblur="isAlphabet(this);">
                <?php
                if (isset($_POST['announcement'])) {
                    echo $_POST['announcement'];
                }
                ?></textarea><br/><?php
                //a radiobutton list is created where the user chooses his sending options. He can either
                //send to all system users, or specific users.
                ?><br/><input type="radio" id="notify_all" name="notify[]" value="all"/><label for="notify_all"><?php echo $lang['radioButton_1']; ?></label><br/>
            <input type="radio" id="notify_sp" name="notify[]" value="sp"/><label for="notify_sp"><?php echo $lang['radioButton_2']; ?></label><br/><br/>
            <input name="confID" type="hidden" value="<?php echo $confID ?>"/> <?php
            //a server side button is created in order to execute the server side
            //script for call for papers posting and sending. A link is added to redirect to a page where
            //I can only choose people from a mailing list
                ?><input type="submit" name="cfp" value="<?php echo $lang['Button_1']; ?>"/> <?php
            //The condition checks that the submit button was clicked
            if (isset($_GET['type'])) {
                //a variable is declared storing the value of the textarea contating the
                //call for papers text
                $ann = mysql_safe($_POST['announcement']);
                //if there was no call for papers text in the textarea, a javascript alert appears
                //prompting the user to type something.
                if (!$ann) {
                    echo '<script>'
                    , 'alert("Please enter a text for the Call for participation");'
                    , '</script>';
                    //In the case where there was text in the textarea
                } else {
                    //A query is executed in order to update the conference record with the same
                    //ID as the passed value with the text for the call for papers
                    mysql_query("UPDATE conference SET call_for_papers_text = '$ann'
                        WHERE conference_id = '$confID'") or die(mysql_error());

                    //the condition checks if a sending option is chosen from the radiobutton list
                    if (isset($_POST['notify'])) {
                        //the value of the radiobutton that was clicked is stored in a variable
                        $who = $_POST['notify'];
                        //if the "allusers" radio was selected
                        if ($who[0] == "all") {
                            //A query is executed to select the email adresses of all system users
                            //and the resultset is stored in a variable.
                            $allMembers = mysql_query("SELECT email FROM member");
                            //each row in the resultset is stored in an associative array on each loop iteration
                            while ($memberRow = mysql_fetch_array($allMembers, MYSQL_ASSOC)) {
                                //the email address of each user is retrieved from the associative
                                //array and placed in a variable email. This variable represents the recipient
                                $email = $memberRow['email'];
                                //a variable "subject" is initialized that represents the subject of the email.
                                $subject = $confName + ' - Call for Papers';
                                //a variable message is initialized containing the content of the email message.
                                $message = <<<EMAIL
                        This is an international Call for participation for the "'$confName'" Conference:\n
                        '$ann' \n
                        For more Information please click this link:
                        arolis.com/confo/main.php?page=conferencePage2.php&conference_id=$confID
EMAIL;
                                //variable "from" specifies the sender
                                $from = $confName + 'Organinzing Committee';
                                //headers specifies the sender of the message
                                $headers = "From:" . $from;
                                //a mail function is called to send the message
                                mail($email, $subject, $message, $headers);
                            }
                            //an alert is shown when the call for papers is sent successfully
                            echo '<script type="text/javascript">'
                            , 'alert("Call for participation sent successfully");'
                            , '</script>';
                        }
                        ////if the specific users radio was selected
                        else if ($who[0] == "sp") {
                            //a header is created, redirecting to the page containing the system mailing list.
                            header('Location: mailingListDiv.php?conference_id=' . $confID . '&callforpapers=' . $ann . '');
                        }
                    } else {
                        //If no sending options were chosen, an error message is alerted
                        echo '<script type="text/javascript">'
                        , 'alert("Please set sending options");'
                        , '</script>';
                    }
                }
            }

            //closes connection to database
            mysql_close();

            //End Documentation
                ?>


        </form>
<?php include "jquery.php"; ?>
        <script type="text/javascript">

            //a function isAlphabet is created taking as a parameter the element
            //doing the action
            function isAlphabet(elem){
                //a variable is initialized that stores a regular expression for letters, numbers and underscores.
                var alphaExp = /^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$/;
                //The condition checks that the value of the element matches the reg-ex
                if(elem.value.match(alphaExp)){
                    // in the case it does, the function returns true.
                    return true;
                } else{
                    //if it doesnt, an alert message is displayed and the
                    //function returns false.
                    alert("please enter text only");
                    return false;
                }

            }

        </script>
        <script type="text/javascript">
            $(document).ready(function(){
                $(document).find(":submit").click(function(){

                    $(document).find("form").attr("action", "callForPapers.php?type=1");

                });
            });

        </script>
    </body>
</html>

